#!/usr/bin/env perl

#########################################################################
#									#
# Name:		rcs_to_pv2						#
#									#
# Args:		--srcfile X[,v] --destfile Y[,pv] [--verbose]		#
#		--srcfile	The name of the source RCS file to	#
#				convert to a PV2 file.			#
#		--destfile	The name of the PV2 file to create.	#
#		--verbose	Show progress information messages.	#
#									#
# Purpose:	Converts the contents of the specified RCS file into	#
#		a PV2 version file. This utility is a wrapper for the	#
#		rcs2pv2 routine available for the  codemgr2 module.	#
#									#
#########################################################################

BEGIN {
	push @INC,$ENV{CODEMGR_LIBS} if exists($ENV{CODEMGR_LIBS});
}

use Getopt::Long;
use codemgr2 qw(logmsg warnmsg errormsg rcs2pv load_config get_project
		 repos1_tag_to_repos2);
use File::Basename;
use File::Find;
use Cwd;
use pv;

if(! -d $ENV{PROJECTS_ROOT}) {
	errormsg("\"PROJECTS_ROOT\" directory \"$PROJECTS_ROOT\" does not exist.",2);
}
$PROJECTS_ROOT=$ENV{PROJECTS_ROOT};


umask 002;
$_PRG=basename($0);
$ARG_verbose=0;

sub usage {
	print "
Usage: $_PRG --srcfile X[,v] --destfile Y[,pv] [--verbose]

or:    $_PRG --srcdir X --destdir Y [--verbose]

or:    $_PRG --project Y [--verbose]

This tool will convert either a single RCS file to a PV2 file, or
a complete directory or RCS files to an equivalent directory of PV2 files.

When the --project argument is specified the complete contents of the
repository for the project are subject to conversion. The .meta
directory is ignored and a repository is created with the name of the
project with '-pv' appended. 

Written by Simon Edwards, 2006.
";
	exit 1;
}

if(!GetOptions(
	"srcdir=s"	=> \$ARG_srcdir,
	"destdir=s"	=> \$ARG_destdir,
	"srcfile=s"	=> \$ARG_srcfile,
	"destfile=s"	=> \$ARG_destfile,
	"project=s"	=> \$ARG_project,
	"verbose"	=> \$ARG_verbose,
	)) {
	usage;
}

#########################################################################
# Ensure the correct permutation of arguments have been given and 	#
# if not show the usage.						#
#########################################################################

$cnt=0;
$cnt+=1 if defined($ARG_srcdir);
$cnt+=2 if defined($ARG_destdir);
$cnt+=4 if defined($ARG_srcfile);
$cnt+=8 if defined($ARG_destfile);
$cnt+=16 if defined($ARG_project);

if($cnt!=3 && $cnt!=12 && $cnt!=16) {
	usage;
}

if($cnt==12) {
	($rc,$msg)=rcs2pv(RCSFILE=>$ARG_srcfile,PVFILE=>$ARG_destfile,VERBOSE=>$ARG_verbose);
	if($rc==0) {
		exit 0;
	}
	errormsg($msg,1);
}

#########################################################################
# Ok - complete directories need to be processed - so ensure both	#
# directories already exist and abort if not.				#
#########################################################################

if($cnt==3) {
	if(! -e $ARG_srcdir) {
		errormsg("Source directory '$ARG_srcdir' does not exist.",1);
	}
	if(! -d $ARG_srcdir) {
		errormsg("Source directory '$ARG_srcdir' is not a directory!",1);
	}

	if(! -e $ARG_destdir) {
		errormsg("Source directory '$ARG_destdir' does not exist.",1);
	}
	if(! -d $ARG_destdir) {
		errormsg("Source directory '$ARG_destdir' is not a directory!",1);
	}

	opendir($dfd,$ARG_srcdir) or 
		errormsg("Unable to read directory '$ARG_srcdir'.",1);

	while($c=readdir($dfd)) {
		next if ! -f "$ARG_srcdir/$c";
		next if "$ARG_srcdir/$c" !~ /,v$/;
		my $s=$c; $s =~ s/,v$//; $s.=",pv";
		next if -e "$ARG_destdir/$s";
		logmsg("Converting '$c' to '$s'.");
		{
			local $ARG_verbose=0;
			($rc,$msg)=rcs2pv(RCSFILE=>"$ARG_srcdir/$c",PVFILE=>"$ARG_destdir/$s");
			if($rc!=0) {
				unlink "$ARG_destdir/$s";
				warnmsg("Unable to complete: $msg",1);
				warnmsg("File will not be converted.");
			}
		}
	}
	closedir($dfd);
	exit(0);
}

if($cnt==16) {
	my $config=load_config("$PROJECTS_ROOT/$ARG_project/.meta");
	my $repos_type=1;
	if(!defined($config)) {
		errormsg("Unable to load project configuration for '$ARG_project.",1);
	}
	if(exists($config->{REPOS_TYPE})) {
		$repos_type=$config->{REPOS_TYPE}->[0];
	}
	if($repos_type==2) {
		errormsg("Source project '$ARG_project' already uses a PV2 repository.",1);
	}

	#################################################################
	# First generate a list of directories to consider under this 	#
	# directory...							#
	#################################################################

	@RR=();
	find( 
		sub {
			if($_ =~ /^\.meta$/) {$File::Find::prune=1;return;}
			if(-d $File::Find::name) {push @RR,$File::Find::name;}
		},
		"$PROJECTS_ROOT/$ARG_project"
	);

	my $x="$PROJECTS_ROOT/$ARG_project/{0,1}";
	@R=map { s/$x//; $_="." if $_ eq ""; $_; } @RR;

	if(! -d "$PROJECTS_ROOT/$ARG_project-pv") {
		mkdir("$PROJECTS_ROOT/$ARG_project-pv",02775) or
			errormsg("Unable to create directory: $!",1);
	}
	for (@R) {
		my $srcdir="$PROJECTS_ROOT/$ARG_project/$_";
		my $destdir="$PROJECTS_ROOT/$ARG_project-pv/$_";

		if(! -d $destdir) {
			mkdir($destdir,02755) or
				errormsg("Unable to create directory: $!",1);
		}

		#########################################################
		# Open this current directory and read all files 	#
		# from it...						#
		#########################################################

		opendir($dfd,$srcdir) or 
			errormsg("Unable to read directory '$srcdir'.",1);

		logmsg("");
		logmsg("Converting files in directory '$_' ...");
		logmsg("");
		while($c=readdir($dfd)) {
			next if ! -f "$srcdir/$c";
			next if "$srcdir/$c" !~ /,v$/;
			my $s=$c; $s =~ s/,v$//; $s.=",pv";
			next if -e "$destdir/$s";
			logmsg("Converting '$c' to '$s'.");
			{
				local $ARG_verbose=0;
				($rc,$msg)=rcs2pv(RCSFILE=>"$srcdir/$c",PVFILE=>"$destdir/$s");
				if($rc!=0) {
					unlink "$destdir/$s";
					warnmsg("Unable to complete: $msg",1);
					warnmsg("File will not be converted.");
				}
			}
		}
		closedir($dfd);
	}

	#########################################################################
	# Now the code has been moved across the next step is to attempt	#
	# to add the tag details, one at a time...				#
	#########################################################################

	if(-d "$PROJECTS_ROOT/$ARG_project/.meta/releases") {
		my ($dfd,$cfile);
		opendir($dfd,"$PROJECTS_ROOT/$ARG_project/.meta/releases") or
			errormsg("Unable to open directory: $!",1);
		while($cfile=readdir($dfd)) {
			next if! -f "$PROJECTS_ROOT/$ARG_project/.meta/releases/$cfile";
			logmsg("Converting tag '$cfile' ...");
			my($rc,$ok,$failed)=repos1_tag_to_repos2(SRCPROJ=>$ARG_project,DESTPROJ=>"$ARG_project-pv",
						TAG=>$cfile);
			if($rc==-1) {
				errormsg("Unable to create tag '$cfile':");
				errormsg($ok);
				next;
			}
			logmsg("  Tag '$cfile' completed [OK: $ok, FAILED: $failed]");
		}
		closedir($dfd);
	}
}


